package com.czl.module_activity.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.module_activity.BR
import com.czl.module_activity.R
import com.czl.module_activity.adapter.ActivityAdapter
import com.czl.module_activity.adapter.BulletinAdapter
import com.czl.module_activity.databinding.FragmentBullentinListBinding
import com.czl.module_activity.viewmodel.BulletinListViewModel

@Route(path = AppConstants.Router.Activities.F_ACTIVITIES_BULLETIN_LIST)
class BulletinListFragment:BaseFragment<FragmentBullentinListBinding,BulletinListViewModel>() {

    private lateinit var mAdapter: BulletinAdapter
    override fun initContentView(): Int {
        return R.layout.fragment_bullentin_list
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("社区公告")
        hiddenBottomShadow()
        initAdapter()
        reload()
    }

    private fun initAdapter() {
        mAdapter = BulletinAdapter(this)
        binding.ryCommon.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }
    override fun initViewObservable() {
        viewModel.uc.refreshListEvent.observe(this, {
            binding.smartCommon.autoRefresh()
        })
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            handleRecyclerviewData(
                data == null,
                data?.records as MutableList<*>?,
                mAdapter,
                binding.ryCommon,
                binding.smartCommon,
                viewModel.currentPage,
                data?.current == data?.pages
            )
        })
        viewModel.uc.toBulletinDetailEvent.observe(this, {
            startContainerActivity(
                AppConstants.Router.Activities.F_ACTIVITIES_BULLENTIN_DETAIL,
                Bundle().apply {
                    putInt(
                        AppConstants.BundleKey.BULLENTIN_ID,
                        it
                    )
                })
        })
        LiveBusCenter.observeBulletinRefreshEvent(this,{
            binding.smartCommon.autoRefresh()
        })
    }

    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }
    override fun onDetach() {
        super.onDetach()
        LiveBusCenter.postBulletinRefreshEvent("")
    }
}